package com.yunding.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunding.dto.EmptyClassDTO;
import com.yunding.dto.StudentClassInfoDTO;
import com.yunding.dto.UpdateClassDto;
import com.yunding.entity.FreeClass;
import com.yunding.mapper.ClassMapper;
import com.yunding.service.EmptyClassService;
import com.yunding.service.StudentService;
import com.yunding.vo.StudentSimpleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guorui
 * @date 2020-10-03 19:54
 */
@Service
@Slf4j
public class EmptyClassServiceImpl extends ServiceImpl<ClassMapper, FreeClass> implements EmptyClassService {
    @Resource
    private ClassMapper classMapper;
    @Resource
    private StudentService studentsService;

    /**
     * 提取出新增和修改空课的公共部分，减少重复代码
     *
     * @param freeClass
     * @param classInfo
     * @return
     */
    public static FreeClass setWeek(FreeClass freeClass, List<String> classInfo) {
        freeClass.setWeek1(classInfo.get(0));
        freeClass.setWeek2(classInfo.get(1));
        freeClass.setWeek3(classInfo.get(2));
        freeClass.setWeek4(classInfo.get(3));
        freeClass.setWeek5(classInfo.get(4));
        freeClass.setWeek6(classInfo.get(5));
        freeClass.setWeek7(classInfo.get(6));
        freeClass.setWeek8(classInfo.get(7));
        freeClass.setWeek9(classInfo.get(8));
        freeClass.setWeek10(classInfo.get(9));
        freeClass.setWeek11(classInfo.get(10));
        freeClass.setWeek12(classInfo.get(11));
        freeClass.setWeek13(classInfo.get(12));
        freeClass.setWeek14(classInfo.get(13));
        freeClass.setWeek15(classInfo.get(14));
        freeClass.setWeek16(classInfo.get(15));
        freeClass.setWeek17(classInfo.get(16));
        freeClass.setWeek18(classInfo.get(17));
        return freeClass;
    }

    /**
     * 新增空课信息
     */
    @Override
    public Boolean registerClass(EmptyClassDTO emptyClassFrom) throws InvocationTargetException, IllegalAccessException {
        String studentId = emptyClassFrom.getStudentId();
        String studentName = emptyClassFrom.getStudentName();
        String studentGroupOrder = emptyClassFrom.getStudentGroup();
        String studentClass = emptyClassFrom.getStudentClass();
//        studentsService.selectStudents(studentId, studentName);
        //集合大小包含着20节课的信息，每节课由18个0或1组成
        List<String> classInfo = new ArrayList<String>();
        for (int j = 0; j < 18; j++) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < 20; i++) {
                Object[] ClassOne = emptyClassFrom.getEmptyClass().get(i);
                if (ClassOne[j]==null) {
                    stringBuffer.append("0");
                } else {
                    stringBuffer.append("1");
                }

            }
            classInfo.add(stringBuffer.toString());
            System.out.println("第"+j+"周："+stringBuffer.toString());
        }
        //classInfo包含某名学生这18周的课程信息，顺序是周一的四节-周二的四节-...-周五的四节
        FreeClass freeClass = new FreeClass();
        freeClass.setStudentId(studentId);
        freeClass.setStudentName(studentName);
        freeClass.setStudentGroup(studentGroupOrder);
        freeClass.setStudentClass(studentClass);
        setWeek(freeClass, classInfo);
        classMapper.insert(freeClass);
        return true;
    }

    /**
     * 修改空课信息
     *
     * @param emptyClassFrom
     * @return
     */
    @Override
    public Boolean updateClass(EmptyClassDTO emptyClassFrom) throws InvocationTargetException, IllegalAccessException {
        String studentId = emptyClassFrom.getStudentId();
        String studentName = emptyClassFrom.getStudentName();
        String studentGroupOrder = emptyClassFrom.getStudentGroup();
        String studentClass = emptyClassFrom.getStudentClass();
//        studentsService.selectStudents(studentId, studentName);
        //集合大小包含着20节课的信息，每节课由18个0或1组成
        List<String> classInfo = new ArrayList<String>();
        for (int j = 0; j < 18; j++) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < 20; i++) {
                Object[] ClassOne = emptyClassFrom.getEmptyClass().get(i);
                if (ClassOne[j]==null) {
                    stringBuffer.append("0");
                } else {
                    stringBuffer.append("1");
                }
            }
            classInfo.add(stringBuffer.toString());
            System.out.println("第"+j+"周："+stringBuffer.toString());
        }
        FreeClass freeClass = new FreeClass();
        freeClass.setStudentId(emptyClassFrom.getStudentId());
        freeClass.setStudentName(emptyClassFrom.getStudentName());
        freeClass.setStudentClass(studentClass);
//      条件构造器,根据student_id查询内容
        UpdateWrapper<FreeClass> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("student_id", studentId);
        freeClass.setStudentGroup(studentGroupOrder);
        setWeek(freeClass, classInfo);
        classMapper.update(freeClass, updateWrapper);
        return true;
    }

    /**
     * 返回指定学生的课程信息
     * 返回一个元素个数为20的list集合。其中每个元素长度为18位
     */
    @Override
    public StudentClassInfoDTO selectClassInfo(UpdateClassDto updateClassDto) throws InvocationTargetException, IllegalAccessException {
        String studentId = updateClassDto.getStudentId();
        String studentName = updateClassDto.getStudentName();
        System.out.println(studentId + "姓名：" + studentName);
        studentsService.selectStudents(studentId, studentName);
        FreeClass freeClass = classMapper.selectById(studentId);
        List<String> classInfo = new ArrayList<String>();
        String string;
        String classInformation;
        for (int i = 0; i < 20; i++) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 0; j < 18; j++) {
                switch (j) {
                    case 0:
                        string = freeClass.getWeek1();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 1:
                        string = freeClass.getWeek2();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 2:
                        string = freeClass.getWeek3();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 3:
                        string = freeClass.getWeek4();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 4:
                        string = freeClass.getWeek5();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 5:
                        string = freeClass.getWeek6();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 6:
                        string = freeClass.getWeek7();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 7:
                        string = freeClass.getWeek8();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 8:
                        string = freeClass.getWeek9();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 9:
                        string = freeClass.getWeek10();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 10:
                        string = freeClass.getWeek11();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 11:
                        string = freeClass.getWeek12();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 12:
                        string = freeClass.getWeek13();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 13:
                        string = freeClass.getWeek14();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 14:
                        string = freeClass.getWeek15();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 15:
                        string = freeClass.getWeek16();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 16:
                        string = freeClass.getWeek17();
                        stringBuffer.append(string.charAt(i));
                        break;
                    case 17:
                        string = freeClass.getWeek18();
                        stringBuffer.append(string.charAt(i));
                        break;
                }
            }
//            System.out.println("第"+i+"节课："+stringBuffer);
//            System.out.println("第"+i+"节,第"+j+"周："+stringBuffer);
            classInformation = stringBuffer.toString();
            classInfo.add(classInformation);
        }
        StudentClassInfoDTO studentClassInfoDTO = new StudentClassInfoDTO();
        studentClassInfoDTO.setStudentId(studentId);
        studentClassInfoDTO.setStudentName(studentName);
        studentClassInfoDTO.setStudentGroup(freeClass.getStudentGroup());
        studentClassInfoDTO.setEmptyClass(classInfo);
        return studentClassInfoDTO;
    }
}
